/*
 * PlayerChecker.java
 *
 * Created on 25.11.2007, 15:58:24
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.server.threads;

import cz.possoft.server.main.ChessServer;

/**
 * Toto vlakno prohlizi pole hracu po 3 sekundach a zasila vsem hracum info o tom, kdo je null atd.
 *
 * @author mara
 */
public class PlayerChecker extends Thread {

    Thread[] players;
    
    private static final int SLEEP_INTERVAL = 3000;

    public PlayerChecker() {
        players = new Thread[ChessServer.MAXPLAYERS];
    }
    
    public void run() {
        try {
            while (true) {

                // cekame 3 sekund pred kazdym spustenim kontroly
                sleep(SLEEP_INTERVAL);
                sendConnectedMessageToAllStatic();
            }
        } catch (InterruptedException ie) {
            System.err.println("Interr. exception v PlayerChecker vlaknu.");
        }
    }

    // vsem hracum zasle CONNECTED: message. (tzn.seznam vsech pripojenych hracu a jejich stav)
    // hracum kteri maji obsazeneho oponenta pak posle null v pripade, ze tento oponent neexistuje
    // tato metoda je volana z vlakna PlayerChecker
    private void sendConnectedMessageToAllStatic() {        
        //getThreadGroup().list();
        int players_count = getThreadGroup().enumerate(players, true);
        players[players_count] = null;        
        ChessServerSocket.setPlayers(players);        
        
         // konec programu, pokud uz neni pripojen zadny klient
        if ((ChessServer.isSomebodyConnectFlag()) && (players_count == ChessServerSocket.FIRST_PLAYER_INDEX)) {
            System.out.println("Vsichni klienti odpojeni.");
            if (ChessServer.KILL_SERVER_WHEN_ALL_PLAYERS_DISCONNECTED) {
            	System.out.println("Koenc programu.");
            	System.exit(0); 
            }
        }
        
        int j = ChessServerSocket.FIRST_PLAYER_INDEX;
        boolean flag = true;
        while (flag) {
            Thread playerPom = ChessServerSocket.getPlayers()[j];
            if ((playerPom != null) && (playerPom instanceof ChessServerSocket)) {
                ChessServerSocket player = (ChessServerSocket) playerPom;
                // prvni hrac zasila vsem CONNECTED message
                if (j == ChessServerSocket.FIRST_PLAYER_INDEX) {
                    player.sendConnectedMessageToAll(true);
                }
                // kontrola soupere. 
                player.check_souper();
                if (player.getSouper() == null) {
                    try {
                    player.sendToClient("NULL: ");
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                
            }
                else {
                flag = false;
            }
            j++;
        }
    }
}
